import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const song = {
    state: {
        isPlay: false, // 播放状态
        buttonIcon: '#icon-bofang', // 播放状态的图标
        url: '', // 歌曲URL
        duration: 0, //  音乐时长
        curTime: 0, // 当前音乐的播放位置
        changeTime: 0, //  指定播放时刻
        autoNext: true, // 用于触发自动播放下一首
        lyric: [], // 未处理的歌词数据
        listOfSongs: [], // 当前歌单列表
        listIndex: null, // 当前歌曲在歌曲列表的位置
    },
    getters: {
        isPlay: state => state.isPlay,
        buttonIcon: state => {
            let buttonIcon = state.buttonIcon
            if (!buttonIcon) {
                buttonIcon = JSON.parse(window.sessionStorage.getItem('buttonIcon') || null)
            }
            return buttonIcon
        },
        url: state => {
            let url = state.url
            if (!url) {
                url = JSON.parse(window.sessionStorage.getItem('url') || null)
            }
            return url
        },
        duration: state => {
            let duration = state.duration
            if (!duration) {
                duration = JSON.parse(window.sessionStorage.getItem('duration') || null)
            }
            return duration
        },
        curTime: state => {
            let curTime = state.curTime
            if (!curTime) {
                curTime = JSON.parse(window.sessionStorage.getItem('curTime') || null)
            }
            return curTime
        },
        changeTime: state => {
            let changeTime = state.changeTime
            if (!changeTime) {
                changeTime = JSON.parse(window.sessionStorage.getItem('changeTime') || null)
            }
            return changeTime
        },
        autoNext: state => {
            let autoNext = state.autoNext
            if (!autoNext) {
                autoNext = JSON.parse(window.sessionStorage.getItem('autoNext') || null)
            }
            return autoNext
        },
        lyric: state => {
            let lyric = state.lyric
            if (lyric.length === 0) {
                lyric = JSON.parse(window.sessionStorage.getItem('lyric') || null)
            }
            return lyric
        },
        listOfSongs: state => {
            let listOfSongs = state.listOfSongs
            if (!listOfSongs.length) {
                listOfSongs = JSON.parse(window.sessionStorage.getItem('listOfSongs') || null)
            }
            return listOfSongs
        },
        listIndex: state => {
            let listIndex = state.listIndex
            if (!listIndex) {
                listIndex = JSON.parse(window.sessionStorage.getItem('listIndex') || null)
            }
            return listIndex
        },

    },
    mutations: {
        setIsPlay: (state, isPlay) => { state.isPlay = isPlay },
        setbuttonIcon: (state, buttonIcon) => { state.buttonIcon = buttonIcon },
        setUrl: (state, url) => { state.url = url },
        setDuration: (state, duration) => { state.duration = duration },
        setCurTime: (state, curTime) => { state.curTime = curTime },
        setChangeTime: (state, changeTime) => { state.changeTime = changeTime },
        setAutoNext: (state, autoNext) => { state.autoNext = autoNext },
        setLyric: (state, lyric) => { state.lyric = lyric },
        setListOfSongs: (state, listOfSongs) => { state.listOfSongs = listOfSongs },
        setListIndex: (state, listIndex) => { state.listIndex = listIndex },
    }
}
const store = new Vuex.Store({
    modules: {
        song
    },
    state: {
        // HOST: 'http://120.79.58.44:8080',
        HOST: 'http://localhost:8080',
    },
    getters: {},
    mutations: {},
    actions: {}
})

export default store
